public class Toint {
    public int StrToInt(String str) {
        if(str.isEmpty()){
            return 0;
        }
        int flag = 1;
        int res = 0;
        char[] arr = str.toCharArray();
        if(str.charAt(0) == '+'){
            flag = 1;
            arr[0] = 0;
        }else if(str.charAt(0) == '-'){
            flag = -1;
            arr[0] = 0;
        }
        for(int i = 0;i<arr.length;i++){
            if(arr[i]>'9' || arr[i] < '0'){
                res = 0;
                break;
            }
            res = res * 10 + arr[i] - '0';

        }
        return flag * res;
    }
}
